<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="categoryName"
            placeholder="请输入分类名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="goodsOptions"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="id"
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="商品名称" prop="userName">
            <el-input
              v-model="queryParams.goodsName"
              placeholder="请输入商品名称"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="商品编码" align="center" key="goodsCode" prop="goodsCode" v-if="columns[0].visible" />
          <el-table-column label="商品名称" align="center" key="goodsName" prop="goodsName" v-if="columns[1].visible" :show-overflow-tooltip="true" />
          <el-table-column label="商品类别" align="center" key="ancestorsName" prop="ancestorsName" v-if="columns[2].visible" :show-overflow-tooltip="true" />
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import {getCategoryTree, listGoods} from "@/api/check/goods";

export default {
  name: "goodsIndex",
  components: { Treeselect },
  data(){
    return {
      loading: false,
      categoryName: '',
      goodsOptions:[],
      //单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 显示搜索条件
      showSearch: true,
      total: 0,
      goodsList: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        ancestors: undefined
      },
      columns: [
        { key: 0, label: `商品编号`, visible: true },
        { key: 1, label: `商品名称`, visible: true },
        { key: 1, label: `商品类别`, visible: true }
      ]
    }
  },
  created() {
    this.categoryTree();
    this.queryParams.ancestors = '1,13'
    this.getList();
  },
  watch: {
    categoryName(val){
      this.$refs.tree.filter(val);
    }
  },
  methods: {
    handleQuery(){
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery(){
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        goodsName: undefined,
        ancestors: undefined
      }
    },
    getList(){
      this.loading = true;
      listGoods(this.queryParams).then(response => {
          this.goodsList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    handleNodeClick(nodeData,node) {
      if (nodeData.children){
        this.queryParams.ancestors = nodeData.id;
      }else {
        this.queryParams.ancestors = node.parent.key + ',' + nodeData.id;
      }
      this.handleQuery();
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    //查询左侧商品分类树
    categoryTree(){
      getCategoryTree().then(response => {
        this.goodsOptions = response.data
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.companyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
  }
}
</script>

<style scoped>

</style>
